//
//  QBVisitorView.swift
//  Wei91BoiOS
//
//  Created by bing on 16/3/21.
//  Copyright © 2016年 bing. All rights reserved.
//

import UIKit

// 1.定义协议，继承和类的格式是不一样的，不是oc<NSObject>,当协定方法为Optional，协议需要加上@obj

@objc protocol QBVisitorViewDelegate: NSObjectProtocol {
    
    // 2.定义方法
    // 注册按钮被点击
    optional func visitorViewDidClickRegisterButton()
    
    // 登陆按钮被点击
    optional func visitorViewDidClickLoginButton()
}

class QBVisitorView: UIView {
    
    // 3.定义代理属性，id<QBVisitorView>
    // 代理需要设置weak，不然可能会循环引用
    weak var delegate: QBVisitorViewDelegate?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        // 只有调用super init对象才会创建完毕
        super.init(frame: frame)
        
        prepareUI()
    }
    
    /**
     设置访客视图
     
     - parameter imageName: 图片名称
     - parameter message:   文字
     */
    func setupVisitor(imageName: String, message: String) {
        
        homeView.hidden = true
        
        // 父控件将一个子控件推到最底部
        self.sendSubviewToBack(coverView)
        
        iconView.image = UIImage(named: imageName)
        
        messageLabel.text = message
    }
    
    /// 开始旋转
    func startRotation() {
        
        let rotation = CABasicAnimation(keyPath: "transform.rotation")
        
        rotation.toValue = M_PI * 2
        rotation.duration = 20
        rotation.repeatCount = MAXFLOAT
        
        // 完成的时候不要移除
        rotation.removedOnCompletion = false
        // 添加到图层上
        iconView.layer.addAnimation(rotation, forKey: nil)
    }
    
    /// 准备UI
    private func prepareUI() {
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        // 1.添加子控制器
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2.添加约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 转轮
        /*
        1.item: 要约束的View
        2.attribute: 要约束的view的属性
        3.relatedBy: 参照
        4.toItem: 参照的View
        */
        // 约束要添加，约束统一添加到父控件上面
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        // 遮罩
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        
        // 小房子
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 消息Label
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 当toItem = nil时, attribute一定要设置为: NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))
        
        // 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
        
    }
    
    // @objc 让系统可以找到swift的方法，系统会自动添加，@objc,系统不会为private修饰的方法添加@objc
    // MARK: - 按钮点击事件
    @objc private func didClickRegisterButton(button: UIButton) {
        
        //print(__FUNCTION__)
        
        // 4.调用代理方法
        // visitorViewDidClickRegisterButton?:表示代理实现这个方法就调用这个方法，没有实现就不调用这个方法
        delegate?.visitorViewDidClickRegisterButton?()
    }
    
    func didClickLoginButton() {
        
        //print(__FUNCTION__)
        // Modal出控制器只有控制器才行
        // 4.调用代理的方法
        delegate?.visitorViewDidClickLoginButton?()
    }
    // MARK: - 懒加载
    /// 转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    /// 遮罩
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    /// 小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    /// 消息Label
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.text = "关注一些人,看看有什么惊喜!!"
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(18)
        label.numberOfLines = 0
        label.sizeToFit()
        
        return label
    }()

    // 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        
        // 设置
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 添加事件
        button.addTarget(self, action: "didClickRegisterButton:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    // 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        
        // 设置
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        // 添加点击事件
        button.addTarget(self, action: "didClickLoginButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}
